﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using 纸片人.UtilFolter;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace 纸片人.CalculateControls
{
    /// <summary>
    /// 帧控件
    /// 有一个输出
    /// 有一个显示
    /// </summary>
    public sealed class FrameControl : CalculateControl
    {
        
        private TextBlock frameCountTextBlock;
        private ExporterDragControl exporterDrag;



        #region 属性
        public double FrameCount
        {
            get { return (double)GetValue(FrameCountProperty); }
            set { SetValue(FrameCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FrameCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FrameCountProperty =
            DependencyProperty.Register("FrameCount", typeof(double), typeof(FrameControl), new PropertyMetadata(0d));

        #endregion 

        public FrameControl()
        {
            this.DefaultStyleKey = typeof(FrameControl);
           
        }

       

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
           
         
            GetControls();

        }

        private void GetControls()
        {
            frameCountTextBlock = GetTemplateChildFormName<TextBlock>(nameof(frameCountTextBlock));
            TemplateControlUtil.CreateBinding(this, "", nameof(FrameCount), frameCountTextBlock, TextBlock.TextProperty);

            exporterDrag = GetTemplateChildFormName<ExporterDragControl>(nameof(exporterDrag));
            exporterDrag.Background = new SolidColorBrush(Colors.CadetBlue);
            exporterDrag.CanDrag = true;
            exporterDrag.DragStarting += exporterDrag_DragStarting;

        }


        private void exporterDrag_DragStarting(UIElement sender, DragStartingEventArgs args)
        {
            OnDragExportInvoke();

            OutputData od = new OutputData();
            od.Type = OutputType.Double;
            od.Exporter = this;
            od.ExportConnectElement =(ExporterDragControl) exporterDrag;
            od.ExporterInfo = nameof(FrameCount);
            EffectOutputControl.outputData = od;
        }



       

        protected override void Timer_Tick(object sender, object e)
        {
                FrameCount++;
        }

        public override void Destroy()
        {
            RemoveAllExport();

            timer.Stop();
            List<ConnectLineUIElement> allConnectElement = new List<ConnectLineUIElement>();
            //添加所有的输出
            allConnectElement.AddRange(ListExportoneConnectElement);
            //删除所有的输入连接
            DeleteAllConnnectInvoke(allConnectElement);
            ListExportoneConnectElement = null;
        }

        public override void RemoveProvider(ConnectLineUIElement connectElement)
        {
            //删除连接线
            RemoveExportoneConnectElement(connectElement);
            if (ListExportoneConnectElement.Count < 1)
            {
                exporterDrag.SetDeConnectState();
            }

        }


    }
}
